package com.xian.im.modules.sensitive;

import com.xian.im.modules.sensitive.handler.SensitiveBloom;

import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: xian-im-server
 * @description:  敏感词过滤工厂，jvm 过滤 redis 敏感词过滤
 * @author: liru.xian
 * @create: 2021-02-23 11:28
 **/
public class SensitiveBloomFactory {

    /**
     *  敏感词容器
     */
    private static Map<String, SensitiveBloom>  sensitiveBloomMap = new ConcurrentHashMap<>();

    static{
        ServiceLoader<SensitiveBloom> loader = ServiceLoader.load(SensitiveBloom.class);
        for (SensitiveBloom bloom : loader) {
            sensitiveBloomMap.put(bloom.getType(),bloom);
        }
    }

    public static SensitiveBloom type(String type){
        return sensitiveBloomMap.get(type);
    }

    public static void initialize(){
        for (String s : sensitiveBloomMap.keySet()) {
          sensitiveBloomMap.get(s).loadSensitive();
        }
    }

}
